

package com.zf.yichat.controller.packet;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.response.MixuanIndexDto;
import com.zf.yichat.mapper.PacketMixuanApiMapper;
import com.zf.yichat.mapper.PacketMixuanMapper;
import com.zf.yichat.model.PacketMixuan;
import com.zf.yichat.service.PacketService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.CheckStatus;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 *
 */
@Controller
@RequestMapping("packet/mixuan")
public class PacketMixuanController extends BaseController {
    @Resource
    PacketMixuanApiMapper packetMixuanApiMapper;

    @Resource
    PacketMixuanMapper packetMixuanMapper;

    @Autowired
    PacketService packetService;

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public String index() {
        return "fanli/mixuan-index";
    }

    @RequestMapping("list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, String name, Integer status) {


        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(packetMixuanApiMapper.selectList(userId, name, status, 0), v -> v);
    }


    @RequestMapping("ad/save")
    @ResponseBody
    public FsResponse setAd(Integer ad, Long id) {

        PacketMixuan packetMixuan = new PacketMixuan();
        packetMixuan.setAd(ad);
        packetMixuan.setId(id);
        packetMixuanMapper.updateByPrimaryKeySelective(packetMixuan);
        return FsResponseGen.success();
    }

    @RequestMapping("sort/save")
    @ResponseBody
    public FsResponse setSort(Integer sort, Long id) {

        PacketMixuan packetMixuan = new PacketMixuan();
        packetMixuan.setSort(sort);
        packetMixuan.setId(id);
        packetMixuanMapper.updateByPrimaryKeySelective(packetMixuan);
        return FsResponseGen.success();
    }


    @RequestMapping("delete")
    @ResponseBody
    public FsResponse delete(Long id) {

        PacketMixuan packetMixuan = new PacketMixuan();
        packetMixuan.setStatus(1);
        packetMixuan.setId(id);
        packetMixuanMapper.updateByPrimaryKeySelective(packetMixuan);
        return FsResponseGen.success();
    }

    @RequestMapping("check")
    @ResponseBody
    public FsResponse check(Long id, Integer status, String reason) {

        PacketMixuan packetMixuan = packetMixuanMapper.selectByPrimaryKey(id);
        Contracts.assertNotNull(packetMixuan, "值不存在");
        //通过
        if (status == 1) {
            //暂停->启动
            if (packetMixuan.getCheckStatus() == CheckStatus.PAUSE.getVal()) {
                return packetService.toggleMixuanAdmin(id);
            } else if (packetMixuan.getCheckStatus() == CheckStatus.FAILED.getVal()) {
                //拒绝->重审
                return packetService.createMixuanAgain(packetMixuan);
            } else {
                //审核通过
                return packetService.createMixuanSuccess(packetMixuan);
            }
        } else if (status == 2) {
            //拒绝
            packetMixuan.setRefuseReason(reason);
            return packetService.createMixuanFail(packetMixuan);
        } else if (status == 3) {
            //终止
            return packetService.terminateMixuanAdmin(id);
        } else if (status == 4) {
            //暂停
            return packetService.toggleMixuanAdmin(id);
        }
        return FsResponseGen.fail();

    }

    @RequestMapping("/detail/index")
    public String detail(Long id) {
        PacketMixuan mixuan = packetMixuanMapper.selectByPrimaryKey(id);
        MixuanIndexDto dto = new MixuanIndexDto();
        getFields(dto, dto.getClass(), mixuan, mixuan.getClass());
        dto.setSmallImagesList2(dto.getSmallImages() == null ? null : JSONArray.parseArray(dto.getSmallImages(), String.class));
        getRequest().setAttribute("packetMixuan", dto);
        return "fanli/mixuan-detail";
    }

    private void getFields(Object classA, Class<?> clazzA, Object classB, Class clazzB) {
        Class<?> superClass = clazzA.getSuperclass();
        if (!superClass.equals(Object.class)) {
            //如果父类不是Objcet类，那表示该类继承了自定义类，则执行递归操作。
            getFields(classA, superClass, classB, clazzB);
        }

        //没有父类，执行以下代码
        Field[] classBFields = clazzB.getDeclaredFields();
        Field claA;
        for (Field classBField : classBFields) {
            classBField.setAccessible(true);
            String name = classBField.getName();
            try {
                //判断当前类是否有次属性名
                PropertyDescriptor pd = new PropertyDescriptor(name, clazzB);
                Method readMethod = pd.getReadMethod();
                claA = clazzA.getDeclaredField(name);
                claA.setAccessible(true);
                claA.set(classA, readMethod.invoke(classB));
            } catch (Exception e) {
                //没有的话跳过此属性
                continue;
            }
        }
    }
}
